package com.wjggy.core.common.util.cookie;

import java.io.UnsupportedEncodingException;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;

import com.wjggy.core.common.util.DateUtil;
import com.wjggy.core.common.util.Md5;
import com.wjggy.core.common.util.preferences.ConfigKeys;
import com.wjggy.core.common.util.preferences.SystemGlobals;
import com.wjggy.core.entities.User;
/**
 * 验证码cookie帮助类
 * @author xiaofan.yu
 * createTime: 2011-11-23 下午09:39:09
 */
public class CookieUtils {
	private static final Logger log = Logger.getLogger(CookieUtils.class);
	/**
	 * 添加验证码
	 * @param servletResponse
	 * @param authCode
	 * @throws UnsupportedEncodingException
	 */
	public static void addAuthCode(HttpServletResponse servletResponse,String authCode){
		CookieHandle.addCookie(servletResponse, 
				SystemGlobals.getValue( ConfigKeys.AUTHCODE_DATA),
				 Md5.getMd5Str(authCode), 
				SystemGlobals.getIntValue( ConfigKeys.AUTHCODE_EFFTIME),
				 SystemGlobals.getValue( ConfigKeys.WEB_NAME));// 设置验证码
			CookieHandle.addCookie(servletResponse, 
					SystemGlobals.getValue( ConfigKeys.AUTHCODE_TIME),
					String.valueOf(DateUtil.getTodayAddMinuteLong(15)),
					SystemGlobals.getIntValue( ConfigKeys.AUTHCODE_EFFTIME),
					 SystemGlobals.getValue( ConfigKeys.WEB_NAME));// 验证码设置过期时间
	}
	
	/**
	 * 验证验证码
	 * @param request
	 * @param authCode
	 * @return
	 */
	public static int checkAuthCode(HttpServletRequest request,String authCode){
		int status=0;
		String longTime = CookieHandle.getCookie(request,SystemGlobals.getValue( ConfigKeys.AUTHCODE_DATA), null);
		String imgData = CookieHandle.getCookie(request, SystemGlobals.getValue(ConfigKeys.AUTHCODE_TIME),null);
		authCode= Md5.getMd5Str(authCode);
		if (longTime == null || !longTime.matches("[0-9]+")|| Long.parseLong(longTime) < DateUtil.getTimeLong()) {// 验证码为空或者读取的不为数字类型或者小于当前时间
			status=0;  //延时操作
		}else if(authCode==null||imgData==null){
			status=3;  //未填写验证码
		}else if(imgData.equals(authCode)){//验证通过
			status=1;
		}else{
			status=2;//未知原因
		}
		return status;
	}
	
	/**
	 * 
	 *  @author xiaofan.yu  
	 *  DateTime 2011-12-11 上午11:48:42
	 *  @see 
	 *  @since 用户登陆 
	 *  describe:采用用户名.管理员独立写入 暂时未写入用户其他数据
	 *  @param servletResponse
	 *  @param user
	 *  @param userTime
	 */
	public static void loginWriter(HttpServletResponse servletResponse,User user,String userTime){
		CookieHandle.addCookie(servletResponse, 
				SystemGlobals.getValue( ConfigKeys.WEB_USER),
				user.getUserlogin(),Integer.parseInt(userTime),
				 SystemGlobals.getValue( ConfigKeys.COOKIE_WEBNAME));//
		CookieHandle.addCookie(servletResponse, 
				SystemGlobals.getValue( ConfigKeys.WEB_USER_ID),
				String.valueOf(user.getId()),Integer.parseInt(userTime),
				 SystemGlobals.getValue( ConfigKeys.COOKIE_WEBNAME));//

		if(user.getIsadmin()){
				CookieHandle.addCookie(servletResponse, 
					SystemGlobals.getValue( ConfigKeys.WEB_ADMIN),
					user.getUserlogin(),Integer.parseInt(userTime),
					 SystemGlobals.getValue( ConfigKeys.COOKIE_WEBNAME));//
		}
	}
	
	public static  User checkUserLogin(HttpServletRequest request){
		String userName = CookieHandle.getCookie(request,SystemGlobals.getValue( ConfigKeys.WEB_USER), null);
		if(userName==null){
			return null;
		}
		String userId = CookieHandle.getCookie(request,SystemGlobals.getValue( ConfigKeys.WEB_USER_ID), null);
		String isAdmin = CookieHandle.getCookie(request,SystemGlobals.getValue( ConfigKeys.WEB_ADMIN), null);
		
		User user =new User();
		user.setUseremail(userName);
		user.setId(Long.parseLong(userId));
		if(isAdmin!=null){
			user.setIsadmin(true);
		}
		return user;
	}
}
